package com.qiangesoft.blog.file.core;

import com.alibaba.fastjson.JSON;
import com.qiangesoft.blog.common.utils.UUIDUtils;
import com.qiangesoft.blog.file.model.FileInfoVo;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * 七牛云文件存储
 */
@Slf4j
@Service
public class QiNiuFileStoreImpl implements FileStoreStrategy {

    private String qi_niu_accessKey;
    private String qi_niu_secretKey;
    private String qi_niu_bucket;
    private Region region;
    private String qi_niu_url;

    @Override
    public FileInfoVo uploadFile(MultipartFile file) throws Exception {
        return null;
    }

    @Override
    public FileInfoVo uploadFile(MultipartFile file, String path) throws Exception {
        String originalFilename = file.getOriginalFilename();
        String fileExtension = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);

        String key = null;
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(region);
        //...其他参数参考类注释
        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传
        Auth auth = Auth.create(qi_niu_accessKey, qi_niu_secretKey);
        String upToken = auth.uploadToken(qi_niu_bucket);
        FileInputStream inputStream = null;
        try {
            inputStream = (FileInputStream) file.getInputStream();
            Response response = uploadManager.put(inputStream, UUIDUtils.getUuid() + "." + fileExtension, upToken, null, null);
            //解析上传成功的结果
            DefaultPutRet putRet = JSON.parseObject(response.bodyString(), DefaultPutRet.class);
            key = qi_niu_url + putRet.key;
        } catch (QiniuException ex) {
            Response r = ex.response;
            log.info("QiniuException:{}", r.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        // todo
        return null;
    }

    @Override
    public FileInfoVo uploadFile(InputStream inputStream, String contentType) throws Exception {
        return null;
    }

    @Override
    public FileInfoVo uploadFile(InputStream inputStream, String contentType, String path) throws Exception {
        return null;
    }

    @Override
    public List<FileInfoVo> batchUploadFile(MultipartFile[] files) throws Exception {
        return null;
    }

    @Override
    public List<FileInfoVo> batchUploadFile(MultipartFile[] files, String path) throws Exception {
        return null;
    }

    @Override
    public void downloadFile(String key, HttpServletRequest request, HttpServletResponse response) throws Exception {

    }

    @Override
    public void deleteFile(String key) throws Exception {

    }
}
